package cn.hotel.servlet;

import cn.hotel.pojo.ClientInformationManagement;
import cn.hotel.service.ClientInformationManagementService;
import cn.hotel.service.ClientInformationManagementServiceImpl;
import cn.hotel.utlis.Paging;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;


@WebServlet("/cims.do")
public class ClientInformationManagementServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        ClientInformationManagementService managementService = new ClientInformationManagementServiceImpl();

        String type = request.getParameter("type");
        type = type == null? "inquire" : type;
        switch (type){
            case "inquire":
                inquires(request,response,managementService);
                break;
            case "change":
                break;
            case "delete":
                deletes(request,response,managementService,out);
                break;
        }
    }
    //删除
    private void deletes(HttpServletRequest request, HttpServletResponse response, ClientInformationManagementService managementService, PrintWriter out) {
        String identityCard = request.getParameter("identityCard");
        String appHref = request.getContextPath();
        Map<String ,Object> objectMap = managementService.deletes(identityCard);
        if("1".equals(objectMap.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appHref+"/cims.do?type=inquire'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='"+appHref+"/cims.do?type=inquire'</script>");
        }
        out.flush();
    }

    //分页查询
    private void inquires(HttpServletRequest request, HttpServletResponse response, ClientInformationManagementService managementService) throws ServletException, IOException {
        Paging<ClientInformationManagement> managementPaging = new Paging<>();

        //获取当前页
        String currents = request.getParameter("current");

        //获取查询值
        String mobilePhone = request.getParameter("mobilePhone");
        //获取数据数量
        if ("".equals(mobilePhone)) {
            mobilePhone = null;
        }
        Map<String,Object> objectMap = managementService.sum(mobilePhone);
        if("1".equals(objectMap.get("flag"))){
            int num = (int) objectMap.get("data");
            managementPaging.setSumData(num);
        }

        //发送当前页
        int nums = StringUtil.strToIntFirst(currents);
        nums = nums > managementPaging.getSumPage()? managementPaging.getSumPage():nums;
        managementPaging.setCurrent(nums);
        //获取数据
        Map<String ,Object> stringObjectMap = managementService.cim(mobilePhone,managementPaging.getCurrent(),managementPaging.getNumData());
        if("1".equals(stringObjectMap.get("flag"))){
            managementPaging.setPagingList((List<ClientInformationManagement>) stringObjectMap.get("data"));
        }
        //传递数据
        request.setAttribute("mobilePhone",mobilePhone);
        request.setAttribute("managementPaging",managementPaging);
        request.getRequestDispatcher("logjsp/clientinformationmanagement.jsp").forward(request,response);
    }
}
